package cn.sw.system.menu.mapper;

import cn.sw.common.database.domain.entity.Menu;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 菜单表 Mapper 接口
 *
 * @author QIUQIU&LL (个人博客:https://)
 * @date 2025-05-09
 * @tags 做一个优秀的全栈架构
 */
@Mapper
public interface MenuMapper extends BaseMapper<Menu> {

    /**
     * 使用递归查询获取指定节点及其所有子节点（支持字符串 id）
     */
    @Select({
            "WITH RECURSIVE MenuTree AS (",
            "   SELECT * FROM sys_menu WHERE id = #{id}",
            "   UNION ALL",
            "   SELECT m.* FROM sys_menu m",
            "   INNER JOIN MenuTree mt ON m.parent_id = mt.id",
            ")",
            "SELECT * FROM MenuTree ORDER BY sort"
    })
    List<Menu> selectRecursiveChildren(@Param("id") String id);
}
